<?php

class TypeChecker {
	
	public static $_EMAIL_TYPE = 'validateEmail';
	public static $_ALNUM_TYPE = 'validateAlnum';
	
	private static function validateEmail($data) {
		$emailValidator = new Zend_Validate_EmailAddress();
		if ($emailValidator->isValid($data)) {
			return true;
		}
		return 'email_invalid';
	}
	
	private static function validateAlnum($data) {
		$alnumValidator = new Zend_Validate_Alnum();
		if ($alnumValidator->isValid($data)) {
			return true;
		}
		return 'not_alnum';
	}
	
	/**
	 * 
	 * Validate parameters and returns :
	 * 		- true if validation is ok
	 * 		- an array('name' => <cause>, ...) if validation failed
	 * @param array of type :
	 * 		array (
	 * 			array(
	 * 				'data' => $data, 
	 * 				'type' => <TYPE>:string,
	 * 				'name' => <field_name>:string
	 * 			),
	 * 			...
	 * 		)
	 */
	static function validate($array) {
		$hasError = false;
		$errorArray = array();
		foreach ($array as $field) {
			$isValid = self::$field['type']($field['data']);
			if ($isValid === true) {
				// ok
			}else{
				// validation error
				$hasError = true;
				$errorArray[$field['name']] = $isValid;
			}
		}
		
		if ($hasError) {
			return $errorArray;
		} else {
			return true;
		}
	}
	
}